﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;

namespace Scrabble.Model.Tests
{
    public static class Extensions
    {
        public static string SquareToString(Tile tile)
        {
            return tile == null ? "." : tile.Letter.ToString();
        }

        public static IEnumerable<char> RowToString(this Board board, int row)
        {
            return Enumerable.Range(0, Board.Columns).Select(n => board[n, row]).Select(tile => tile == null ? '.' : tile.Letter);
        }
    }

    [Binding]
    public class Steps
    {
        private Game Game { get; set; }

        [Given(@"the following players are staring a new game:")]
        public void GivenTheFollowingPlayersAreStaringANewGame(Table table)
        {
            Game = new Game(1, table.Rows.Select(row => row[0]).ToArray());
        }

        [Given(@"I have a new game played by (.+)")]
        public void GivenIHaveANewGamePlayedBy(string playerName)
        {
            Game = new Game(1, new[] { playerName } );
        }

        [When(@"(.+) plays (.+) to (.+)")]
        public void WhenPlayOccurs(string playerName, string letters, string inVector)
        {
            var player = Game.ActivePlayer;
            player.PlaceTiles(player.CurrentTiles.Take(5).ToArray(), PlayVector.Parse(inVector));

            Trace.WriteLine(Game.Board.RowToString(7));
        }

        [Then(@"(.+) should have a score of (\d+)")]
        public void ThenPlayersScoreShouldBe(string playerName, int score)
        {
            Assert.AreEqual(score, Game.PreviousPlayer.CurrentScore);
        }

        [Then(@"row (\d+) of the board should be '([\.A-Z]{15})'")]
        public void ThenRowShouldMatch(int row, string layout)
        {
            Assert.AreEqual(layout, new String(Game.Board.RowToString(row - 1).ToArray()));
        }

        [Then(@"the active player should be (.+)")]
        public void ThenTheActivePlayerShouldBe(string playerName)
        {
            Assert.AreEqual(playerName, Game.ActivePlayer.Name);
        }

    }
}
